<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            容器归档
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">容器条码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请扫描容器条码" id="ctBarcode" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li>

        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info aui-btn-block" onclick="save()" id="submitButton">归档</div>
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="margin-left:5%;width:100%">扫描容器数：</div>
                    <div class="aui-list-item-input" style="margin-left:5%">
                        <input type="text" id="num" value="" disabled="true" style='color:red' />
                    </div>
                </div>
            </li>
        </ul>
        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;margin-bottom:20%;" id="tbodyHtml">
        </div>
        <div style="background-color:white; height: 100%;width: 100%;margin-top:3%;border:solid 1px #D3D3D3;display:none;margin-bottom:50px;"
            id="tableDiv">
            <table class="table" style='width:100%; font-size:14px;' id="table">
                <tbody>
                    <tr>
                        <td style="width:20%;text-align:right;">容器条码:</td>
                        <td id="ctBarcodetital"></td>
                    </tr>

                    <tr>
                        <td style="width:20%;text-align:right;">当前位置:</td>
                        <td id="cellCodetital"></td>
                    </tr>
                    <tr>
                        <td style="width:20%;text-align:right;">归档位置:</td>
                        <td id="ext13tital"></td>
                    </tr>
                    <tr>
                        <td style="width:20%;text-align:right;">数量:</td>
                        <td id="qtytital"></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/clickThrottle.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var html = ""; //全局拼接HTML
    var arrSave = [];
    var num = 0;
    var array = new Array();
    // var delArray = new Array();
    //初始化查询仓库下拉框
    apiready = function () {

        $("#ctBarcode").focus();
        idSelector = "ctBarcode";
        //查询条码解析规则
        initScanRule();
        $("#num").val(0);
    }

    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }
    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {

            if (idSelector == 'ctBarcode') {
                //容器条码
                $("#ctBarcode").val(ret);
                tmSplit(ret);
            }
        });
    }

    //保存按钮
    function save() {
        if (!clickThrottle()) {
            return;
        }
        const submitButton = document.getElementById('submitButton');

        submitButton.style.pointerEvents = "none"

        //容器条码非空校验
        if (array == null || array == "" || array == undefined) {
            api.toast({
                msg: '请扫描 容器条码！',
                duration: 3000
            });
            submitButton.style.pointerEvents = "auto"
            return;
        }
        api.showProgress({
            title: '归档中...',
        });
        var array1 = [];
        for (var i = 0; i < array.length; i++) {
            ctBarcode = array[i].ctBarcode
            array1.push(ctBarcode);
        }
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/containerArchiveSave";
        var params = new Object();
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                ctBarcodes: array1, ///容器条码数组
                un: un
                // ,ctBarRedis:JSON.stringify(ctBarRedis) //有array1，直接传参
                ,ctBarRedis:JSON.stringify(array1)
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    api.hideProgress();
                    submitButton.style.pointerEvents = "auto"
                    api.toast({
                        msg: "归档成功",
                        duration: 3000
                    });
                    html = "";
                    //清空表单
                    $("#ctBarcode").val("");
                    $("#ctBarcode").focus();
                    idSelector = "ctBarcode";
                    num = 0
                    $("#num").val(0);
                    /*   //表格数据赋值
                      var list = myobj.list;
                      console.log(list.length);
  
                      var html = "";
                      var ctBarcodetital = "";
                      var cellCodetital = "";
                      var qtytital = "";
                      var ext13tital = "";
                      for (var i = 0; i < list.length; i++) {
  
                          html += "<div id = '" + ctBarcode + "' class='aaui-btn-info aui-btn-block' style='margin-bottom: 10px;color: black !important;background-color: #addbef !important;height: 110px;padding: 0.2rem 0.2rem;font-size: 0.7rem;'>";
                          html += "<table style='width:100%;'>";
                          html += "<tr style='width:100%;'><td style='width:30%;'>容器条码：</td><td  id='ctBarcodetital' ' colspan='2'>" + list[i].ctBarcodetital + "</td></tr>";
                          html += "<tr style='width:100%;'><td style='width:30%;'>当前位置：</td> <td  id='cellCodetital' ' colspan='3'>" + list[i].cellCodetital + "</td></tr>";
                          html += "<tr style='width:100%;'><td style='width:30%;'>归档位置：</td><td id='ext13tital' ' >" + list[i].ext13tital + "</td></tr>";
                          html += "<tr style='width:100%;'><td style='width:15%;'>数量：</td><td id='qtytital' >" + list[i].qtytital + "</td></tr>";
                          html += "</table></div>";
  
                      }
                      $("#tableDiv").css('display', 'none');
                      $api.html($api.byId("tbodyHtml"), html) */
                    array = [];
                    //清空表格数据
                    getView();
                } else if (myobj.code == '1') {

                    api.hideProgress();
                    submitButton.style.pointerEvents = "auto"

                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });

                    $("#ctBarcode").val("");
                    $("#ctBarcode").focus();
                    idSelector = "ctBarcode";

                    $("#ctBarcode").focus();
                }
            } else {
                $("#ctBarcode").val("");
                $("#ctBarcode").select();
                idSelector = "ctBarcode";
                api.hideProgress();
                submitButton.style.pointerEvents = "auto"
                alert("连接超时，请耐心等待");
            }
        });
    }

    //自动保存
    function saveAuto() {
        //容器条码
        var ctBarcode = $("#ctBarcode").val();
        //容器条码非空校验
        if (ctBarcode == "" || ctBarcode == null || ctBarcode == undefined) {
            return;
        }
        //保存
        save();
    }

    //
    function tmSplit(sm) {
        var ctBarcode = $("#ctBarcode").val();
        var array1 = [];
        for (var i = 0; i < array.length; i++) {
            ctBarcode1 = array[i].ctBarcode
            array1.push(ctBarcode1);
        }
        if (array1.includes(ctBarcode)) {
            $("#ctBarcode").val("");
            $("#ctBarcode").select();
            api.toast({
                msg: "容器条码已经扫描!",
                duration: 3000
            });
            return;
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisContainer";
        var data = {
            values: {
                tm: sm, //条码
                ctBarcode: ctBarcode,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    var map = myobj.data;
                    array.push(map);

                    getView();
                    // console.log(map);
                    // var ctBarcode = "";
                    // var cellCode = "";
                    // var qty = "";
                    // var ext13 = "";
                    // for (var key in map) {

                    //     if ("ctBarcode" == key) {
                    //         ctBarcode = map[key];
                    //         console.log(ctBarcode);
                    //     }
                    //     if ("cellCode" == key) {
                    //         cellCode = map[key];
                    //     }
                    //     if ("qty" == key) {
                    //         qty = map[key];
                    //     }
                    //     if ("ext13" == key) {
                    //         ext13 = map[key];
                    //     }
                    // }
                    // console.log(array.includes(ctBarcode));

                    // console.log(array.length);
                    // if (!delArray.includes(ctBarcode)) {
                    // array.push(ctBarcode);
                    // html += "<div id = '" + ctBarcode + "' class='aaui-btn-info aui-btn-block' style='margin-bottom: 10px;color: black !important;background-color: #addbef !important;height: 110px;padding: 0.2rem 0.2rem;font-size: 0.7rem;'>";
                    // html += "<table style='width:100%;'>";
                    // html += "<tr style='width:100%;'><td style='width:30%;'>容器条码：</td><td  id='ctBarcodetital' ' colspan='2'>" + ctBarcode + "</td><td><button style = 'background-color:red;color:white' onclick = del('" + ctBarcode + "')>删除</button></td></tr>";
                    // html += "<tr style='width:100%;'><td style='width:30%;'>当前位置：</td> <td  id='cellCodetital' ' colspan='3'>" + cellCode + "</td></tr>";
                    // html += "<tr style='width:100%;'><td style='width:30%;'>归档位置：</td><td id='ext13tital' ' >" + ext13 + "</td></tr>";
                    // html += "<tr style='width:100%;'><td style='width:15%;'>数量：</td><td id='qtytital' >" + qty + "</td></tr>";
                    // html += "</table></div>";

                    // $("#tableDiv").css('display', 'none');
                    // $api.html($api.byId("tbodyHtml"), html);

                    // for (var k = 0; k < delArray.length; k++) {
                    //     var divToDelete = document.getElementById(delArray[k]);
                    //     divToDelete.parentNode.removeChild(divToDelete);
                    // }
                    $("#ctBarcode").select();
                    num = num + 1;
                    $("#num").val(num);
                    // }
                } else if (myobj.code == '1') {

                    $("#ctBarcode").val("");
                    $("#ctBarcode").select();
                    $api.html($api.byId("tbodyHtml"), html);
                    getView();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //箱号监听回车事件
    $("#ctBarcode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                $("#ctBarcode").blur();
                //调用条码解析+保存事件
                tmSplit(tm);
            }
        }
    });

    function del(ctBarcode) {
        // delArray.push(ctBarcode);
        array = array.filter(function (item) { return item.ctBarcode != ctBarcode });
        num = num - 1;
        $("#num").val(num);
        //刷新视图
        getView();
    }
    function getView() {
        var html = "";
        for (var i = 0; i < array.length; i++) {

            html += "<div id = '" + array[i].ctBarcode + "' class='aaui-btn-info aui-btn-block' style='margin-bottom: 10px;color: black !important;background-color: #addbef !important;height: 110px;padding: 0.2rem 0.2rem;font-size: 0.7rem;'>";
            html += "<table style='width:100%;'>";
            html += "<tr style='width:100%;'><td style='width:30%;'>容器条码：</td><td  id='ctBarcodetital' ' colspan='2'>" + array[i].ctBarcode + "</td><td><button style = 'background-color:red;color:white' onclick = del('" + array[i].ctBarcode + "')>删除</button></td></tr>";
            if (array[i].cellCode == undefined) {
                html += "<tr style='width:100%;'><td style='width:30%;'>当前位置：</td> <td  id='cellCodetital' ' colspan='3'>" + "" + "</td></tr>";
            } else {
                html += "<tr style='width:100%;'><td style='width:30%;'>当前位置：</td> <td  id='cellCodetital' ' colspan='3'>" + array[i].cellCode + "</td></tr>";
            }
            if (array[i].ext13 == undefined) {
                html += "<tr style='width:100%;'><td style='width:30%;'>归档位置：</td> <td  id='ext13tital' ' colspan='3'>" + "" + "</td></tr>";
            } else {
                html += "<tr style='width:100%;'><td style='width:30%;'>归档位置：</td><td id='ext13tital' ' >" + array[i].ext13 + "</td></tr>";
            }
            html += "<tr style='width:100%;'><td style='width:15%;'>数量：</td><td id='qtytital' >" + array[i].qty + "</td></tr>";
            html += "</table></div>";
        }
        $api.html($api.byId("tbodyHtml"), html);
    }
    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>